package pers.cj.pdos.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import pers.cj.pdos.controller.base.BaseController;
import pers.cj.pdos.controller.entity.ResponseData;
import pers.cj.pdos.controller.entity.ResponseEntity;
import pers.cj.pdos.controller.entity.ResponseList;
import pers.cj.pdos.entity.Company;
import pers.cj.pdos.entity.ProviderType;
import pers.cj.pdos.service.IProviderTypeService;
import pers.cj.pdos.utils.DataUtil;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author chenj
 * @since 2019-12-12
 */
@Controller
@RequestMapping("/provider")
public class ProviderTypeController extends BaseController {
    @Autowired
    IProviderTypeService providerTypeService;

    @RequestMapping("/providerType")
    @RequiresPermissions("providerType:query")
    public String customType() {
        return "/provider/providerType";
    }

    @ResponseBody
    @RequiresPermissions("providerType:query")
    @RequestMapping("/providerType/getList")
    public ResponseList getList(int page, int limit) {
        Company company = getCompany();

        List<ProviderType> list = providerTypeService.listPage(page, limit, company.getId());

        //返回数据
        return ResponseList.success(providerTypeService.count(company.getId()), list);
    }

    @ResponseBody
    @RequiresPermissions("providerType:insert")
    @RequestMapping("/providerType/add")
    public ResponseEntity add(@RequestBody ProviderType providerType) {
        Company company = getCompany();
        if (providerTypeService.checkName(providerType.getName(),company.getId())){
            return new ResponseEntity(2,"名称已存在");
        }
        DataUtil.processEntity(company,providerType);
        providerTypeService.save(providerType);
        return ResponseEntity.success();
    }

    @ResponseBody
    @RequiresPermissions("providerType:close")
    @RequestMapping("/providerType/delete")
    public ResponseEntity delete(@RequestBody ProviderType providerType) {
        try {
            DataUtil.processEntity(false,getCompany(),providerType);
            providerTypeService.removeById(providerType.getId());
            return ResponseEntity.success();
        } catch (Exception e) {
            return ResponseEntity.fail("已被使用无法删除");
        }
    }

    @ResponseBody
    @RequiresPermissions("providerType:insert")
    @RequestMapping("/providerType/update")
    public ResponseData update(@RequestBody ProviderType providerType) {
        Company company = getCompany();
        ProviderType old = providerTypeService.getById(providerType.getId());
        if (!old.getName().equals(providerType.getName()) && providerTypeService.checkName(providerType.getName(),company.getId())){
            return ResponseData.fail("名称已存在");
        }

        DataUtil.processEntity(false,company,providerType);
        providerTypeService.updateById(providerType);
        return ResponseData.success(providerType);
    }

    @ResponseBody
    @RequestMapping("/providerType/getAll")
    public ResponseData getAll() {
        Company company = getCompany();
        //返回数据
        return ResponseData.success(providerTypeService.list(new QueryWrapper<ProviderType>().eq("company_id",company.getId())));
    }
}
